package org.lovebugs.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;

public class DensityHp {
	/**
	 * 根据手机的分辨率�?dp 的单�?转成�?px(像素)
	 */
	public static int dipToPx(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率�?px(像素) 的单�?转成�?dp
	 */
	public static int pxToDip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
	
	/*获取屏幕的实际宽*/
	public static Rect screenRect(Activity a){
		Rect rect = new Rect();
		DisplayMetrics dm = new DisplayMetrics(); 
		a.getWindowManager().getDefaultDisplay().getMetrics(dm); 
		int w = Float.valueOf(dm.widthPixels*dm.density).intValue();
		int h = Float.valueOf(dm.heightPixels*dm.density).intValue();
		rect.set(0, 0, w, h);
		return rect;
	}
	
	/*获取屏幕的实际宽*/
	public static Rect screenRect(Context c){
		Rect rect = new Rect();
		final WindowManager windowManager = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics dm = new DisplayMetrics(); 
		windowManager.getDefaultDisplay().getMetrics(dm); 	
		int w =  Float.valueOf(dm.widthPixels).intValue();
		int h = Float.valueOf(dm.heightPixels).intValue();
		rect.set(0, 0, w, h);
		return rect;
	}
	
	
	/*获取屏幕的实际宽*/
	public static Rect screenPixelsRect(Activity a){
		Rect rect = new Rect();
		DisplayMetrics dm = new DisplayMetrics(); 
		a.getWindowManager().getDefaultDisplay().getMetrics(dm); 
		int w = Float.valueOf(dm.widthPixels).intValue();
		int h = Float.valueOf(dm.heightPixels).intValue();
		rect.set(0, 0, w, h);
		return rect;
	}

	/*获取字体大小*/
	public static float textSize(Activity a, int pixels){
		DisplayMetrics dm = new DisplayMetrics(); 
		a.getWindowManager().getDefaultDisplay().getMetrics(dm); 
		float wx = dm.densityDpi/dm.scaledDensity;
		float sp = (pixels*wx)/dm.densityDpi;
		return sp;
	}
	

}
